package com.cy.pj.sys.service.serviceImpl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.vo.Node;
import com.cy.pj.sys.dao.AdminRoleDao;
import com.cy.pj.sys.dao.MenuDao;
import com.cy.pj.sys.dao.RoleMenuDao;
import com.cy.pj.sys.pojo.Menu;
import com.cy.pj.sys.service.MenuService;
import com.cy.pj.sys.vo.AdminMenuVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author shuidehuiyi@gmail.com
 * @date 2020/6/13 15:57
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleMenuDao roleMenuDao;

    @Autowired
    private AdminRoleDao adminRoleDao;

    @Cacheable(value = "menuCache") //具体业务操作由aop实现(@Cacheable描述的方法为切入点)
    @Override
    public List<Map<String, Object>> findObjects() {
        System.out.println("SysMenuServiceImpl.findObjects");
        List<Map<String, Object>> list = menuDao.findObjects();
//        for (Map<String, Object> stringObjectMap : list) {
//            System.out.println(stringObjectMap.keySet());
//            System.out.println(stringObjectMap.entrySet());
//        }
        if(list==null||list.size()==0) {
            throw new ServiceException("没有对应菜单信息");
        }
        return list;
    }

    @RequiresPermissions("admin:menu")
    @CacheEvict(value = "menuCache")
    @Override
    public int deleteObjects(Integer id) {
        // 1 验证数据合法性
        if(id==null || id<= 0) {throw new IllegalArgumentException("请先选择!");}
        // 2 基于id进行子元素查询
        int childCount = menuDao.getChildCount(id);
        // 3 判断是否有子菜单
        if(childCount > 0 ){throw new ServiceException("请先删除子菜单");}
        // 3 如果没有子菜单则删除角色 菜单关系数据
        roleMenuDao.deleteObjectsByMenuId(id);
        // 4 删除菜单自身信息
        int rows = menuDao.deleteObjects(id);
        // 处理 没有数据的异常
        if(rows==0){ throw new ServiceException("此菜单可能已经不存在"); }
        // 5 返回结果
        return rows;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<Node> findZtreeMenuNodes() {
        return menuDao.findZtreeMenuNodes();
    }

    @CacheEvict(value = "menuCache")
    @Override
    public int saveObject(Menu entity) {
        if(entity==null){throw new ServiceException("保存对象不能为空");}
        if(StringUtils.isEmpty(entity.getName())){
            throw new ServiceException("菜单名不能为空");
        }
        int rows;
        try {
            rows= menuDao.insertObject(entity);
        } catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("保存失败");
        }
        return rows;
    }

    @CacheEvict(value = "menuCache")
    @Override
    public int updateObject(Menu entity) {
        if(entity==null){throw new ServiceException("保存对象不能为空");}
        if(StringUtils.isEmpty(entity.getName())) {throw new ServiceException("菜单名不能为空");}
        int rows = menuDao.updateObject(entity);
        if(rows==0){throw new ServiceException("记录可能已经不存在");}
        return rows;
    }


    @Override
    public List<AdminMenuVo> findAdminMenusByAdminId(Integer id){
        //1.对用户id进行判断
        //2.基于用户id查找用户对应的角色id
        List<Integer> roleIds= adminRoleDao.findRoleIdsByAdminId(id);
        //3.基于角色id获取角色对应的菜单信息,并进行封装.
        List<Integer> menuIds= roleMenuDao.findMenuIdsByRoleIds(roleIds);
        //4.基于菜单id获取用户对应的菜单信息并返回
        return menuDao.findMenusByIds(menuIds);
    }


}
